<?php
/*
 *      Copyright 2009 Nolan Baker <HendersonHasselbalch@gmail.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

function used($word, $id){
    $con = mysql_connect("localhost", "jjenning", "");
    mysql_query("use zendo;", $con);
    $array = mysql_query("SELECT word FROM word_game WHERE game_id = "
                        .$id." AND word = '".$word."';", $con);
    while ($row = mysql_fetch_array($array)) {
        return 1;
    }
    return 0;
}
function isWord($word){
    $con = mysql_connect("localhost", "jjenning", "");
    mysql_query("use zendo;", $con);
    $word = strtolower($word);
    $word = mysql_query("SELECT word FROM words WHERE word = '".$word."';", $con);
    while ($row = mysql_fetch_array($word)) {
        return 1;
    }
    return 0;
}
function isBuddha($word, $rule){
    $con = mysql_connect("localhost", "jjenning", "");
    mysql_query("use zendo;", $con);
    $word = strtolower($word);
    $rule = mysql_query("SELECT r".$rule." FROM words WHERE word = '".$word."';", $con);
    while ($row = mysql_fetch_array($rule)) {
        return $row[0];
    }
}
function randWord($rule, $nature){
    $con = mysql_connect("localhost", "jjenning", "");
    mysql_query("use zendo;", $con);
    $word_array = mysql_query("SELECT word FROM words where r".$rule."=".$nature." ORDER BY Rand() LIMIT 1;", $con);
    while ($row = mysql_fetch_array($word_array)) {
        return $row['word'];
    }
}
function addWord($word, $buddha, $correct = 1, $order = 0){
    $con = mysql_connect("localhost", "jjenning", "");
    mysql_query("use zendo;", $con);
    $sql = "INSERT INTO word_game (word, game_id, w_order, buddha, correct) VALUES ('"
            .$word."',".$_SESSION['game'].",".$order.",".$buddha.",".$correct.");";
    mysql_query($sql, $con);
}
function startsWithS($word){ # 0
    $word = strtolower($word);
    return substr($word, 0, 1) == "s";
}

function dubLet($word){ # 1
    $word = strtolower($word);
    $let = "";
    for($i = 0; $i < strlen($word); $i++){
        $s = substr($word, $i, 1);
        if ($let == $s) {
            return 1;
        }
        $let = $s;
    }
    return 0;
}

function beginEnd($word){ # 2
    $word = strtolower($word);
    return substr($word, 0, 1) == substr($word, strlen($word) - 1, 1);
}

function palindrome($word){ # 3
    $word = strtolower($word);
    for($i = 0; $i < (strlen($word) / 2); $i++){
        if (substr($word, $i, 1) != substr($word, strlen($word) - ($i + 1), 1)){
            return 0;
        }
    }
    return 1;
}

function subWord($word){ # 4
    $word = strtolower($word);
    $len = strlen($word);
    for($i = 0; $i < $len; $i++){
        for($j = 1; $j < $len + 1 - $i; $j++){
            $s = substr($word, $i, $j);
            if (isWord($s) and $s != $word){
                return 1;
            }
        }
    }
    return 0;
}

function endInLastHalf($word){ # 5
    $word = strtolower($word);
    return substr($word, strlen($word) - 1, 1) > "m";
}

function distinctVowels($word){ # 6
    $word = strtolower($word);
    $vowels = "aeiou";
    $v1 = "";
    for($i = 0; $i < 5; $i++){
        $cur_v = substr($vowels, $i, 1);
        for($j = 0; $j < strlen($word); $j++){
            if ($cur_v == substr($word, $j, 1)){
                if ($v1 == ""){
                    $v1 = $cur_v;
                    break;
                }
                return 1;
            }
        }
    }
    return 0;
}

function multipleOf3($word){ # 7
    return strlen($word) % 3 == 0;
}
?>
